package com.ruoyi.common.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.sms.entity.YytSmsCoupon;
import com.ruoyi.common.sms.dao.YytSmsCouponMapper;
import com.ruoyi.common.sms.entity.YytSmsCouponHistory;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import com.ruoyi.common.sms.service.YytSmsCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* <p>
    * 优惠券信息 服务实现类
    * </p>
*
* @author MrPing
* @since 2022-05-31
*/
@Service
public class YytSmsCouponServiceImpl extends ServiceImpl<YytSmsCouponMapper, YytSmsCoupon> implements YytSmsCouponService {

    @Autowired
    private YytSmsCouponMapper yytSmsCouponMapper;

    @Autowired
    private YytMemberService yytMemberService;

    @Autowired
    private YytSmsCouponHistoryService yytSmsCouponHistoryService;

    @Override
    public List<YytSmsCoupon> getShopSysCoupon(String shopId) {
        return yytSmsCouponMapper.getShopSysCoupon(shopId);
    }

    @Override
    public List<YytSmsCoupon> getMyCouponList(String memberId, Integer useType) {
        return yytSmsCouponMapper.getMyCouponList(memberId, useType);
    }

    @Override
    public List<YytPmsSkuInfo> getSkuListByCouponId(String couponId) {
        return yytSmsCouponMapper.getCouponByIdSkuList(couponId);
    }

    @Override
    public boolean addCoupon(YytMember yytMember) {
        QueryWrapper<YytMember> wrapper = new QueryWrapper<>();
        wrapper.eq("wechat_id", yytMember.getWechatId());
        YytMember one = yytMemberService.getOne(wrapper, false);

        List<String> list = new ArrayList<>();
        list.add("1569966862833250306");
        list.add("1569985087897825281");
        list.add("1569985596788535298");
        list.add("1569985866041880577");
        QueryWrapper<YytSmsCoupon> wrapper1 = new QueryWrapper<>();
        wrapper1.in("id", list);
        List<YytSmsCoupon> coupons = super.list(wrapper1);
        QueryWrapper<YytSmsCouponHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", one.getId()).in("coupon_id", list);
        List<YytSmsCouponHistory> histories = yytSmsCouponHistoryService.list(queryWrapper);
        if (histories.size() > 0){
            return false;
        }
        for (YytSmsCoupon coupon : coupons) {
            YytSmsCouponHistory history = new YytSmsCouponHistory();
            history.setCouponId(coupon.getId());
            history.setMemberId(one.getId());
            history.setMemberNickName(one.getNickname());
            history.setGetType(0);
            history.setCreateTime(LocalDateTime.now());
            history.setUseType(0);
            history.setUseMode(0);
            history.setBalance(coupon.getAmount());
            yytSmsCouponHistoryService.save(history);

            UpdateWrapper<YytSmsCoupon> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", coupon.getId()).setSql("receive_count = receive_count + 1");
            super.update(updateWrapper);
        }
        return true;
    }
}
